/*
 *   Author:
 *   vyouzhi <vyouzhi@163.com>
 *   http://www.xda.cn
 *
 *   File:                    proc_data_lib.c
 *   Create Date:      2011-08-11 10:51:22
 */

#include "proc_data_lib.h"

long setData(const char *sql){
	long lastid = 0, u_id = 0;
	int is_normal, is_store, is_master;
	SQL_CONN *sconn;

	is_normal = atoi(conf_get("normal"));
	if(1 == is_normal){
		if ((sconn = connect_sql()) == NULL) return(-1);
		lastid = set_mysql(sql, sconn);
		if(-1 == lastid) return (-1);

	}

	is_store = atoi(conf_get("store"));
	if(1 == is_store){
		u_id = dostore(sql);
	}

	is_master = atoi(conf_get("master"));

	if(1 == is_master)	
		doconvey(u_id);

	return lastid;	
}

char *getid(long id, int type){
	char lastid[1024]; 
	char *sd;

	lastid[0] = '\0';
    if(type == 1)
    	sprintf(lastid, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><Row0><id>%ld</id></Row0></data>", id);
    else
        sprintf(lastid, "[{\"id\":%ld}]", id);

	sd = strdup(lastid);

	return sd;
}

DICT_LINK *select_mysql(char *sql, int type){
	char *dosql = sql;
	DICT_LINK *dict_link_head;
	SQL_CONN *sm_conn;
	
	sm_conn = connect_sql();

	if(sm_conn == NULL) return NULL;
	dict_link_head = get_mysql(dosql, type, sm_conn);

	return dict_link_head;
}

char *getData(recv_xml *rxml){
	char *val, *key;
	int i = 0, extime = 0;
    long start_time, end_time;
	recv_xml *gdxml = rxml;
    DICT_LINK *dict_link_head;

	val = NULL;
	key = gdxml->key;
	extime = gdxml->extime;

	if(key != NULL && 0 != strcmp(key, "null") && extime > 0){
		val = get_memcache(gdxml->key);
		i = 1;
	}
	if(val != NULL) return val;

	start_time = get_sec();
	dict_link_head = select_mysql(gdxml->sub, gdxml->type);
    end_time = get_sec();

    sta_add_base_link(gdxml->sub, end_time-start_time);

    if(dict_link_head == NULL){
		return NULL;
	}else{
		val = set_xml(dict_link_head);
		dict_free(dict_link_head);
	}

	if(i == 1 && val != NULL){
		set_memcache(val, gdxml->key, extime, strlen(val));
	}

	return val;
}

char *JsonData(char *sql, char *key,int extime, int type){
    char *val=NULL;
    int i = 0;
    double start_time, end_time;
    DICT_LINK *dict_link_head;

    if(key != NULL && 0 != strcmp(key, "null") && extime >0){
        val = get_memcache(key);
        i = 1;
    }

    if(val != NULL) return val;

    start_time = get_sec();
    dict_link_head = select_mysql(sql, type);
    end_time = get_sec();
    sta_add_base_link(sql, end_time-start_time);

    if(dict_link_head == NULL){
		return NULL;
	}else{
		val = Json_Set(dict_link_head);
		dict_free(dict_link_head);
	}

    if(i == 1 && val != NULL){
        set_memcache(val, key, extime, strlen(val));
    }
    return val;
}

void FreeXml(recv_xml *fxml){
	SQLFree(fxml->rw);
	SQLFree(fxml->sub);
	SQLFree(fxml->key);
	SQLFree(fxml->host);
	SQLFree(fxml->action);
	SQLFree(fxml);	
}

char *SwitchData(char *s_d, int sw_len, char *format){
    if(memcmp(format, "2", 1)==0){
        return JsonParser(s_d);
    }else if(memcmp(format,"1", 1)==0){
        return XmlParser(s_d, sw_len);
    }
    else{
        d_log("format error value !\n");
        return NULL;
    }
}

char *JsonParser(char *s_d){
    json_t *jproot; 
    char *rw ,*sqltmp=NULL, *sql=NULL, *key, *res=NULL, *tmp;
    int extime=0, type=0, returnid=0, lastid=0;

    jproot = Json_Root(s_d);
    if(jproot == NULL) return NULL;
    
    rw = Json_Find_Tree("rw", jproot);
    if(rw == NULL){
        Json_Free_Root(jproot);
        return NULL;
    }

    sqltmp = Json_Find_Tree("sub", jproot);
    sql = json_unescape(sqltmp);
    free(sqltmp);
    if(sql != NULL){
        if(0 == strcmp(rw, "r")){
            key = Json_Find_Tree("key", jproot);
            tmp = Json_Find_Tree("extime", jproot);
            if(tmp != NULL) {
                extime = atoi(tmp);
                free(tmp);
                tmp = NULL;
            }
            tmp = Json_Find_Tree("type", jproot);
            if(tmp != NULL){
                type = atoi(tmp);
                free(tmp);
                tmp = NULL;
            }
            res = JsonData(sql, key, extime, type);
            free(key);
        }else if (0 == strcmp(rw, "w")){
            lastid = setData(sql);
            if(lastid == -1) return NULL;
            tmp = Json_Find_Tree("returnid", jproot);
            if(tmp != NULL){
                returnid = atoi(tmp);
                free(tmp);
            }
		    if(lastid > 0 && returnid == 1) res = getid(lastid, 2);
        }else if(strcmp(rw, "s") == 0){
            res = sta_get_link();
        }else return NULL; 
    }


    free(rw);
    free(sql);
    Json_Free_Root(jproot);

    return res;
}

char *XmlParser(char *s_d, int sw_len){
	recv_xml *rxml;
	int lastid;
	char *senddata;
	
	senddata = NULL;
	rxml = get_xml(s_d, sw_len);

	/*printf("sub %s\n", rxml->sub);*/
	if(rxml == NULL) return NULL;

	if(rxml->rw == NULL){
		FreeXml(rxml);
		return NULL;
	}

	if(rxml->sub != NULL && strcmp(rxml->rw, "w")==0){
		lastid = setData(rxml->sub);	
		if(lastid > 0 && rxml->returnid == 1) senddata = getid(lastid, 1);

	}else if(rxml->sub != NULL && !strcmp(rxml->rw, "r") && strlen(rxml->sub) > 0){	
		senddata = getData(rxml);
	}else if(!strcmp(rxml->rw, "u")){
		getupdate(rxml->lastid);
		/*recode = 1;
		senddata = NULL; */
	}else if(!strcmp(rxml->rw, "g")){
		updateing(rxml->lastid, rxml->host, rxml->port);
	}else if(!strcmp(rxml->rw, "i")){
		printf("insert log and insert table\n");
		/*insert_log(rxml->sub, rxml->id, rxml->logname);*/
	}else if(rxml->action != NULL && !strcmp(rxml->rw, "m") && !strcmp(rxml->action, "del")){
		/*del_mem(rxml->key);*/
	}else if(strcmp(rxml->rw, "s") == 0){
        senddata = sta_get_link();
    }

	FreeXml(rxml);
	return senddata;
}


/* vim: set ts=4 sw=4: */

